<?php

namespace App\Console\Commands;


use App\Model\Job\Job;
use Ido\Tools\ClassGenerator\GenerateHelper;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Util\Tpl;

class CurdGenerator extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generate:curd {fullClass}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $fullClass = $this->argument('fullClass');
        $job = new $fullClass();
        $colums = $job->getConnection()->getSchemaBuilder()->getColumnListing($job->getTable());
        $class = GenerateHelper::getClassName($fullClass);
        $varName = Str::camel($class);

        $createColumns = $this->notKeys($colums, ['id','uid', 'created_at', 'updated_at']);
        $updateColumns = $this->notKeys($colums, ['id','uid', 'created_at', 'updated_at']);;
        $selectColumns = $this->notKeys($colums, ['uid','created_at', 'updated_at']);

        $routeName = str_replace('_', '-', Str::snake($varName));


        $data = compact('fullClass', 'class', 'varName', 'routeName', 'createColumns', 'updateColumns',
            'selectColumns');
        if(in_array('uid',$colums)){
            $code = Tpl::parse(file_get_contents(resource_path('/codetpl/curd.tpl')), $data);
        }else{
            $code = Tpl::parse(file_get_contents(resource_path('/codetpl/curd_no_uid.tpl')), $data);
        }

        $this->info($code);

//大驼峰
// Str::studly
//snake_snake
// Str::snake()
//小驼峰
// Str::camel()

    }

    public function notKeys($arr, $delValues)
    {
        $rs = [];
        foreach ($arr as $v) {
            if (in_array($v, $delValues)) {
                continue;
            }
            $rs[] = $v;
        }


        return $rs;
    }


}